package server;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.BorderFactory;
import javax.swing.ButtonGroup;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ScrollPaneConstants;
import javax.swing.table.AbstractTableModel;

public class StatPanel extends JPanel{
	private static final long serialVersionUID = 1L;
	
	public enum SelectableEnum {
		WEB_HISTORY ("Web History"),
		PROFIT ("Profit"),
		MONTH ("Month"),
		WEEK ("Week"),
		DAY ("Day");
		
		private String label;
		SelectableEnum(String label){
			this.label = label;
		}
		
		public String getLabel(){
			return label;
		}
	}

	
	SelectableEnum currentlySelectedStat = SelectableEnum.PROFIT;
	SelectableEnum currentlySelectedPeriod = SelectableEnum.DAY;
	
	JPanel topPanel;
	JPanel bottomPanel;
	
	JRadioButton visitedWebPageHistoryRadioButton;
	JRadioButton profitRadioButton;
	
	JRadioButton monthRadioButton;
	JRadioButton weekradioButton;
	JRadioButton dailyRadioButton;
	
	JTable resultTable;
	JTable visitedWebPageHistoryTable;
	
	public StatPanel(){
		setLayout(new BorderLayout());
		createTopPanel();
		createBottomPanel();
	}
	
	private void createTopPanel(){
		topPanel = new JPanel();
		topPanel.setLayout(new BorderLayout());
		add(topPanel, BorderLayout.NORTH);
		
		visitedWebPageHistoryRadioButton = new JRadioButton(SelectableEnum.WEB_HISTORY.getLabel());
		profitRadioButton = new JRadioButton(SelectableEnum.PROFIT.getLabel());
		profitRadioButton.setSelected(true);
		
		ButtonGroup group1 = new ButtonGroup();
		group1.add(profitRadioButton);
		group1.add(visitedWebPageHistoryRadioButton);
		
		dailyRadioButton = new JRadioButton(SelectableEnum.DAY.getLabel());
		dailyRadioButton.setSelected(true);
		weekradioButton = new JRadioButton(SelectableEnum.WEEK.getLabel());
		monthRadioButton = new JRadioButton(SelectableEnum.MONTH.getLabel());
		
		ButtonGroup group2 = new ButtonGroup();
		group2.add(dailyRadioButton);
		group2.add(weekradioButton);
		group2.add(monthRadioButton);
		
		JPanel topLeft = new JPanel();
		topPanel.add(topLeft, BorderLayout.WEST);
	
		topLeft.setLayout(new GridLayout(2, 1, 10, 10));
		topLeft.add(new JLabel("   Information :            ", JLabel.RIGHT));
		topLeft.add(new JLabel(     "   Period :            ", JLabel.RIGHT));
		
		JPanel topCenter = new JPanel();
		topPanel.add(topCenter, BorderLayout.CENTER);
		topCenter.setPreferredSize(new Dimension(380, 50));
		
		JPanel topBottom = new JPanel();
		topBottom.setBorder(BorderFactory.createMatteBorder(0, 0, 3, 0, Color.gray));
		topPanel.add(topBottom, BorderLayout.SOUTH);
		topBottom.setPreferredSize(new Dimension(10, 10));
		
		JPanel topCenterTop = new JPanel();
		JPanel topCenterBottom = new JPanel();
		topCenter.setLayout(new GridLayout(2, 1, 10, 10));
		topCenter.add(topCenterTop);
		topCenter.add(topCenterBottom);
		
		topCenterTop.setLayout(new GridLayout(1, 3));
		topCenterTop.add(profitRadioButton);
		topCenterTop.add(visitedWebPageHistoryRadioButton);
		topCenterTop.add(new JLabel());
		
		topCenterBottom.setLayout(new GridLayout(1, 3));
		topCenterBottom.add(dailyRadioButton);
		topCenterBottom.add(weekradioButton);
		topCenterBottom.add(monthRadioButton);
		
		visitedWebPageHistoryRadioButton.addActionListener(new Group1Action());
		profitRadioButton.addActionListener(new Group1Action());
		monthRadioButton.addActionListener(new Group1Action());
		weekradioButton.addActionListener(new Group1Action());
		dailyRadioButton.addActionListener(new Group1Action());
	}
	
	private void createBottomPanel(){
		bottomPanel = new JPanel();
		
		resultTable = new JTable(new TableModel(SelectableEnum.PROFIT, SelectableEnum.DAY));
		
		JScrollPane scroll = new JScrollPane(resultTable);
		scroll.setPreferredSize(new Dimension(500, 440));
		scroll.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
		scroll.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
		
		bottomPanel.add(scroll);
		
		add(bottomPanel, BorderLayout.CENTER);
	}
	
	private class TableModel extends AbstractTableModel{
		private static final long serialVersionUID = 1L;
		
		ArrayList<RowData<String>> rowList = new ArrayList<RowData<String>>();

		public TableModel(SelectableEnum statType, SelectableEnum period){
			switch(statType){
				case PROFIT:
					switch(period){
					case DAY:
						rowList.add(new ProfitRowData("Jan/1/2012", "50.00$"));
						rowList.add(new ProfitRowData("Jan/2/2012", "50.00$"));
						rowList.add(new ProfitRowData("Jan/3/2012", "50.00$"));
						rowList.add(new ProfitRowData("Jan/4/2012", "50.00$"));
						rowList.add(new ProfitRowData("Jan/5/2012", "50.00$"));
						rowList.add(new ProfitRowData("Jan/6/2012", "50.00$"));
						rowList.add(new ProfitRowData("Jan/7/2012", "50.00$"));
						rowList.add(new ProfitRowData("...", "..."));
						break;
					case WEEK:
						rowList.add(new ProfitRowData("Jan/1/2012 - Jan/7/2012", "350.00$"));
						rowList.add(new ProfitRowData("Jan/8/2021 - Jan/14/2021", "450.00$"));
						rowList.add(new ProfitRowData("Jan/15/2012 - Jan/21/2012", "550.00$"));
						rowList.add(new ProfitRowData("Jan/15/2012 - Feb/3/2012", "700.00$"));
						break;
					case MONTH:
						rowList.add(new ProfitRowData("Jan 2012", "2000.00$"));
						rowList.add(new ProfitRowData("Feb 2012", "50.00$"));
						break;
					default:
						rowList.add(new ProfitRowData("Error", "Error"));
					}
					break;
				case WEB_HISTORY:
					switch(period){
					case DAY:
						rowList.add(new WebHistoryRowData("Jan/1/2012", "www.website1.com", "2"));
						rowList.add(new WebHistoryRowData("Jan/1/2012", "www.website2.com", "4"));
						rowList.add(new WebHistoryRowData("Jan/1/2012", "www.website3.com", "5"));
						rowList.add(new WebHistoryRowData("Jan/2/2012", "www.website1.com", "5"));
						rowList.add(new WebHistoryRowData("Jan/2/2012", "www.website2.com", "6"));
						rowList.add(new WebHistoryRowData("Jan/2/2012", "www.website3.com", "7"));
						rowList.add(new WebHistoryRowData("Jan/2/2012", "www.website1.com", "7"));
						rowList.add(new WebHistoryRowData("Jan/2/2012", "www.website2.com", "4"));
						break;
					case WEEK:
						rowList.add(new WebHistoryRowData("Jan/1/2012 - Jan/7/2012", "www.website1.com", "55"));
						rowList.add(new WebHistoryRowData("Jan/1/2012 - Jan/7/2012", "www.website2.com", "104"));
						rowList.add(new WebHistoryRowData("Jan/1/2012 - Jan/7/2012", "www.website3.com", "75"));
						rowList.add(new WebHistoryRowData("Jan/8/2021 - Jan/14/2021", "www.website1.com", "89"));
						rowList.add(new WebHistoryRowData("Jan/8/2021 - Jan/14/2021", "www.website2.com", "110"));
						rowList.add(new WebHistoryRowData("Jan/8/2021 - Jan/14/2021", "www.website3.com", "77"));
						rowList.add(new WebHistoryRowData("Jan/8/2021 - Jan/14/2021", "www.website4.com", "10"));
						rowList.add(new WebHistoryRowData("...", "...", "..."));
						break;
					case MONTH:
						rowList.add(new WebHistoryRowData("Jan 2012", "www.website1.com", "360"));
						rowList.add(new WebHistoryRowData("Jan 2012", "www.website2.com", "477"));
						rowList.add(new WebHistoryRowData("Jan 2012", "www.website3.com", "400"));
						rowList.add(new WebHistoryRowData("Jan 2012", "www.website4.com", "50"));
						break;
					default:
						rowList.add(new WebHistoryRowData("Error", "Error", "Error"));
				}
					break;
			}
		}

		public String getColumnName(int n){
			if(n == 1){
				return currentlySelectedStat.getLabel();
			}else if(n == 0){
				return currentlySelectedPeriod.getLabel();
			}else if(n == 2){
				return "Hits";
			}
			return "INVALID";
		}

		public int getColumnCount() { 
			switch(currentlySelectedStat){
				case PROFIT:
					return 2;
				case WEB_HISTORY:
					return 3;
			}
			return 0;
		}

		public int getRowCount() { 
			return rowList.size();
		}

		public Object getValueAt(int row, int col) { 
			RowData<String> rowData = rowList.get(row);
			return rowData.get(col);
		}

		// Abstract representation of a table row.
		private abstract class RowData<T>{
			ArrayList<T> list = new ArrayList<T>();
			
			public T get(int col){
				return list.get(col);
			}
		}

		// A profit table row.
		private class ProfitRowData extends RowData<String>{
			public ProfitRowData(String date, String info){
				list.add(date);
				list.add(info);
			}

			public String get(int col){
				return list.get(col);
			}
		}

		// A web history table row.
		private class WebHistoryRowData extends RowData<String>{
			public WebHistoryRowData(String date, String site, String count){
				list.add(date);
				list.add(site);
				list.add(count);
			}
		}

	}
	
	private class Group1Action implements ActionListener{

		@Override
		public void actionPerformed(ActionEvent e) {
			if(visitedWebPageHistoryRadioButton.isSelected()){
				currentlySelectedStat = SelectableEnum.WEB_HISTORY;
				resultTable.setModel(new TableModel(SelectableEnum.WEB_HISTORY , currentlySelectedPeriod));
			}else if(profitRadioButton.isSelected()){
				currentlySelectedStat = SelectableEnum.PROFIT;
				resultTable.setModel(new TableModel(SelectableEnum.PROFIT, currentlySelectedPeriod));
			}
			
			if(monthRadioButton.isSelected()){
				currentlySelectedPeriod = SelectableEnum.MONTH;
				resultTable.setModel(new TableModel(currentlySelectedStat, SelectableEnum.MONTH));
			}else if(weekradioButton.isSelected()){
				currentlySelectedPeriod = SelectableEnum.WEEK;
				resultTable.setModel(new TableModel(currentlySelectedStat, SelectableEnum.WEEK));
			}else if(dailyRadioButton.isSelected()){
				currentlySelectedPeriod = SelectableEnum.DAY;
				resultTable.setModel(new TableModel(currentlySelectedStat, SelectableEnum.DAY));
			}
			
			repaint();
			
		}
		
	}

}
